#ifndef NETWORK_SERVERCONNECTION_H_
#define NETWORK_SERVERCONNECTION_H_

#include "UDPIO.h"

#ifdef _WIN32
	#include <windows.h>
	#include <sys/types.h>
	#include <winsock.h>
#else
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <netdb.h>
#endif

#include <sstream>
#include <iostream>
#include <string>

namespace Network {

class ServerConnection : public UDPIO {
	enum State {
		NOT_CONNECTED,
		CONNECTING,
		CONFIGURING,
		TIME_SYNC,
		LOADING,
		AWAITING_GAMESTATE,
		STATE_INGAME
	};

	std::string host_;
	int port_;

	std::istringstream sin_;
	std::ostringstream sout_;

	State state_;
	unsigned char uid_;
public:
	ServerConnection();
	virtual ~ServerConnection();

	std::istringstream& getInputString();
	std::ostringstream& getOutputStream();

	bool init(std::string host, int port = PORT);
	bool connect();

	// Wysyla zawartosc strumienia do serwera
	void write();

	// Czytazawartosc strumienia wejscia
	void read();

	// Zamyka polaczenie
	void close();

//	InetAddress getInetAddress();
	int getPort();
	void setPort(int port);

	bool isClosed();
};

} //namespace Network

#endif /* NETWORK_SERVERCONNECTION_H_ */
